package com.mrd.shiptask_shipowner.serviecs.initdatadao;

import java.util.HashMap;
import java.util.Map;

import com.mrd.shiptask_shipowner.activity.ConfirmOrderListActivity;
import com.mrd.shiptask_shipowner.activity.ElectronManifestActivity;
import com.mrd.shiptask_shipowner.activity.ElectronManifestListActivity;
import com.mrd.shiptask_shipowner.activity.MainActivity;
import com.mrd.shiptask_shipowner.activity.MakeToOrderListActivity;
import com.mrd.shiptask_shipowner.activity.NewsActivity;
import com.mrd.shiptask_shipowner.activity.ShipOrderActivity;
import com.mrd.shiptask_shipowner.activity.ViewPictureActivity;
import com.mrd.shiptask_shipowner.constants.HttpConstant;
import com.mrd.shiptask_shipowner.constants.database.DBUtil;
import com.mrd.shiptask_shipowner.serviecs.BizNetworkService;
import com.mrd.shiptask_shipowner.shiareutil.SharePreferenceUtil;
import com.mrd.shiptask_shipowner.unit_tools.FunctionOfActivity;
import com.mrd.shiptask_shipowner.unit_tools.HttpUrlConstant;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class InitDatasDaoImpl {
	private static String result;
	private static ProgressDialog progressDialog;
	private static Context thisContext;

	/**
	 * 请求结果处理
	 */
	public static Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			try {
				try {
					progressDialog.dismiss();
				} catch (Exception e3) {

				}
				Bundle data = msg.getData();
				if (!data.isEmpty()) {
					try {
						switch (msg.what) {
						case HttpUrlConstant.INIT_SHIP_ORDER_LIST:
							if (!"ispass".equals(data.getString("msg"))) {
								FunctionOfActivity.makeToaskShow(thisContext,
										data.getString("msg"));
							}
							break;
						case HttpUrlConstant.SHIP_ORDER:
							if (data.getBoolean("isPassOrNot")) {
								getShipOrderDatas(thisContext);
							}
							FunctionOfActivity.makeToaskShow(thisContext,
									data.getString("msg"));
							break;
						case HttpUrlConstant.SUBMIT_YAN_QI:
							FunctionOfActivity.makeToaskShow(thisContext,
									data.getString("msg"));
							if ("isPass".equals(data.get("submit_result"))) {
								DBUtil.updateShipOrderYanQiById(thisContext,
										data.get("followId").toString());
								// thisContext.startActivity(new Intent(
								// thisContext, ShipOrderActivity.class));
								getShipOrderDatas(thisContext);
							}
							break;
						case HttpUrlConstant.SUBMIT_YI_PEI_HUO:
							FunctionOfActivity.makeToaskShow(thisContext,
									data.getString("msg"));
							if ("isPass".equals(data.get("submit_result"))) {
								DBUtil.updateShipOrderPeiHuoById(thisContext,
										data.get("followId").toString());
								// thisContext.startActivity(new Intent(
								// thisContext, ShipOrderActivity.class));
								getShipOrderDatas(thisContext);
							}
							break;
						case HttpUrlConstant.MAKE_TO_ORDER:
							FunctionOfActivity.makeToaskShow(thisContext,
									data.getString("msg"));
							if ("isPass".equals(data.get("submit_result"))) {
								DBUtil.saveMakeToShipOrderDb(thisContext,
										data.getString("result"));
								thisContext.startActivity(new Intent(
										thisContext,
										MakeToOrderListActivity.class));
							} else {
								thisContext.startActivity(new Intent(
										thisContext, MainActivity.class));
							}
							break;
						case HttpUrlConstant.AGREE_ORDER:
							if (data.getBoolean("isPassOrNot")) {
								getShipMakeToOrderDatas(thisContext);
							}
							FunctionOfActivity.makeToaskShow(thisContext,
									data.getString("msg"));
							break;
						case HttpUrlConstant.DISAGREE_ORDER:
							if (data.getBoolean("isPassOrNot")) {
								getShipMakeToOrderDatas(thisContext);
							}
							FunctionOfActivity.makeToaskShow(thisContext,
									data.getString("msg"));
							break;
						case HttpUrlConstant.ElECTRON_MANIFEST_LIST:
							FunctionOfActivity.makeToaskShow(thisContext,
									data.getString("msg"));
							if ("isPass".equals(data.get("submit_result"))) {
								DBUtil.saveElectronManifestDb(thisContext,
										data.getString("result"));
								thisContext.startActivity(new Intent(
										thisContext,
										ElectronManifestListActivity.class));
							} else {
								thisContext.startActivity(new Intent(
										thisContext, MainActivity.class));
							}
							break;
						case HttpUrlConstant.AGREE_ELECTRO:
							if (data.getBoolean("isPassOrNot")) {
								getElectronMainifestDatas(thisContext);
							}
							FunctionOfActivity.makeToaskShow(thisContext,
									data.getString("msg"));
							break;
						case HttpUrlConstant.DISAGREE_ELECTRO:
							if (data.getBoolean("isPassOrNot")) {
								getElectronMainifestDatas(thisContext);
							}
							FunctionOfActivity.makeToaskShow(thisContext,
									data.getString("msg"));
							break;
						case HttpUrlConstant.CONFIRMLIST:
							FunctionOfActivity.makeToaskShow(thisContext,
									data.getString("msg"));
							if ("isPass".equals(data.get("submit_result"))) {
								DBUtil.saveConfirmOrderDb(thisContext,
										data.getString("result"));
								thisContext.startActivity(new Intent(
										thisContext,
										ConfirmOrderListActivity.class));
							} else {
								thisContext.startActivity(new Intent(
										thisContext, MainActivity.class));
							}
							break;
						case HttpUrlConstant.SHIP_OWNER:
							FunctionOfActivity.makeToaskShow(thisContext,
									data.getString("msg"));
							if ("isPass".equals(data.get("submit_result"))) {
								DBUtil.saveConfirmOrderDb(thisContext,
										data.getString("result"));
								thisContext.startActivity(new Intent(
										thisContext,
										ConfirmOrderListActivity.class));
							}
							break;
						case HttpUrlConstant.CONFIRM_SIGN_INT:
							FunctionOfActivity.makeToaskShow(thisContext,
									data.getString("msg"));
							Intent intn = new Intent(thisContext,
									ViewPictureActivity.class);
							intn.putExtra("result", data.getString("result"));
							intn.putExtra("jumpType",
									data.getString("jumpType"));
							thisContext.startActivity(intn);
							break;
						case HttpUrlConstant.SUBMIT_CONRIME_ORDER:
							if (data.getBoolean("isPassOrNot")) {
								getConfirmOrderDatas(thisContext);
							}
							FunctionOfActivity.makeToaskShow(thisContext,
									data.getString("msg"));
							break;
						
						case HttpUrlConstant.SUBMIT_REQUEST_CANCEL_ELE:
							FunctionOfActivity.makeToaskShow(thisContext,
									data.getString("msg"));
							if (data.getBoolean("isPassOrNot")) {
								getElectronMainifestDatas(thisContext);
							}
							break;
						default:
							break;
						}
					} catch (Exception e) {
						FunctionOfActivity.makeToaskShow(thisContext, "操作异常");
					}
				}
			} catch (Exception e) {
			}
		};
	};

	/**
	 * 获取我的船源列表
	 * 
	 * @param context
	 * @return
	 */
	public static String getShipOrderDatas(Context context) {
		thisContext = context;
		try {
			// 此处try catch是为了处理个列表下拉刷新生成了新的线程，而UI在线程中会报错，导致刷新失败
			progressDialog = new ProgressDialog(thisContext);
			progressDialog.setMessage("正在加载，请稍候...");
			progressDialog.show();
		} catch (Exception e) {
		}
		try {
			new Thread(new BizNetworkService(context, handler,
					HttpUrlConstant.INIT_SHIP_ORDER_LIST, null)).start();
		} catch (Exception e) {
			context.startActivity(new Intent(context, ShipOrderActivity.class));
		}
		return result;
	}

	/**
	 * 提交我的船源
	 * 
	 * @param context
	 * @param ShipId
	 * @param getLoadton
	 * @param getStartPort
	 * @param getEndPort
	 * @param getEndTime
	 * @param getCargoTypes
	 * @param getHPrice
	 * @param getLPrice
	 * @param getEShipPort
	 * @param getEShipTime
	 * @param shipName
	 * @param taskId
	 */
	public static void submitShipOrderAction(final Context context,
			final String ShipId, final String getLoadton,
			final String getStartPort, final String getEndPort,
			final String getEndTime, final String getCargoTypes,
			final String getHPrice, final String getLPrice,
			final String getEShipPort, final String getEShipTime,
			final String shipName, final String taskId) {
		thisContext = context;
		progressDialog = new ProgressDialog(thisContext);
		progressDialog.setMessage("提交中,请稍候···");
		progressDialog.show();
		try {
			final HashMap<String, Object> param = new HashMap<String, Object>();
			param.put("shipId", ShipId);
			param.put("loadton", getLoadton);
			param.put("startPort", getStartPort);
			param.put("endPort", getEndPort);
			param.put("endTime", getEndTime);
			param.put("cargoTypes", getCargoTypes);
			param.put("hPrice", getHPrice);
			param.put("lPrice", getLPrice);
			param.put("eShipPort", getEShipPort);
			param.put("eShipTime", getEShipTime);
			param.put("name", shipName);
			param.put("taskId", taskId);
			new Thread(new BizNetworkService(context, handler,
					HttpUrlConstant.SHIP_ORDER, param)).start();
		} catch (Exception e) {
			progressDialog.dismiss();
			FunctionOfActivity.makeToaskShow(context, "提交异常");
		}
	}

	public static void submitShipOrderYiPeiHuo(final Context context,
			String demandId) {
		try {
			thisContext = context;
			// TODO Auto-generated method stub
			progressDialog = new ProgressDialog(thisContext);
			progressDialog.setMessage("正在提交，请稍候···");
			progressDialog.show();
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("demandId", demandId);
			new Thread(new BizNetworkService(context, handler,
					HttpUrlConstant.SUBMIT_YI_PEI_HUO, params)).start();
		} catch (Exception e) {
			// TODO: handle exception
			progressDialog.dismiss();
			FunctionOfActivity.makeToaskShow(context, "提交异常");

		}
	}

	/**
	 * 提交延期
	 * 
	 * @param context
	 * @param demandId
	 * @param dateTime
	 */
	public static void submitShipOrderYanQi(Context context, String demandId,
			String dateTime) {
		try {
			thisContext = context;
			// TODO Auto-generated method stub
			progressDialog = new ProgressDialog(thisContext);
			progressDialog.setMessage("正在提交，请稍候···");
			progressDialog.show();
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("newDate", dateTime);
			params.put("demandId", demandId);
			new Thread(new BizNetworkService(context, handler,
					HttpUrlConstant.SUBMIT_YAN_QI, params)).start();
		} catch (Exception e) {
			// TODO: handle exception
			progressDialog.dismiss();
			FunctionOfActivity.makeToaskShow(context, "提交异常");

		}
	}

	/**
	 * 获取货运申请信息
	 * 
	 * @param context
	 */
	public static String getShipMakeToOrderDatas(Context context) {
		// TODO Auto-generated method stub
		thisContext = context;
		try {// 此处try catch是为了处理个列表下拉刷新生成了新的线程，而UI在线程中会报错，导致刷新失败
			progressDialog = new ProgressDialog(thisContext);
			progressDialog.setMessage("正在加载，请稍候...");
			progressDialog.show();
		} catch (Exception e) {
		}
		try {
			new Thread(new BizNetworkService(context, handler,
					HttpUrlConstant.MAKE_TO_ORDER, null)).start();
		} catch (Exception e) {
			progressDialog.dismiss();
			FunctionOfActivity.makeToaskShow(context, "获取货运申请数据异常");
		}
		return result;
	}

	/**
	 * 接受货运申请
	 * 
	 * @param context
	 * @param getId
	 */
	public static void acceptMakeToOrderAction(Context context, String getId) {
		// TODO Auto-generated method stub
		thisContext = context;
		progressDialog = new ProgressDialog(thisContext);
		progressDialog.setMessage("正在提交，请稍候...");
		progressDialog.show();
		SharePreferenceUtil spu = new SharePreferenceUtil(context,
				HttpConstant.SAVE_USER);
		try {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("id", getId);
			param.put("ship_id", spu.getShip_id());
			new Thread(new BizNetworkService(context, handler,
					HttpUrlConstant.AGREE_ORDER, param)).start();
		} catch (Exception e) {
			progressDialog.dismiss();
			FunctionOfActivity.makeToaskShow(context, "接受货运申请异常");
		}
	}

	/**
	 * 拒绝货运申请
	 * 
	 * @param context
	 * @param getId
	 */
	public static void refuseMakeToOrderAction(Context context, String getId) {
		// TODO Auto-generated method stub
		thisContext = context;
		progressDialog = new ProgressDialog(thisContext);
		progressDialog.setMessage("正在提交，请稍候...");
		progressDialog.show();
		SharePreferenceUtil spu = new SharePreferenceUtil(context,
				HttpConstant.SAVE_USER);
		try {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("id", getId);
			param.put("ship_id", spu.getShip_id());
			new Thread(new BizNetworkService(context, handler,
					HttpUrlConstant.DISAGREE_ORDER, param)).start();
		} catch (Exception e) {
			progressDialog.dismiss();
			FunctionOfActivity.makeToaskShow(context, "拒绝货运申请异常");
		}
	}

	/**
	 * 获取运输合同数据
	 * 
	 * @param context
	 */
	public static String getElectronMainifestDatas(Context context) {
		// TODO Auto-generated method stub
		thisContext = context;
		try {// 此处try catch是为了处理个列表下拉刷新生成了新的线程，而UI在线程中会报错，导致刷新失败
			progressDialog = new ProgressDialog(thisContext);
			progressDialog.setMessage("正在加载，请稍候...");
			progressDialog.show();
		} catch (Exception e) {
		}
		try {
			new Thread(new BizNetworkService(context, handler,
					HttpUrlConstant.ElECTRON_MANIFEST_LIST, null)).start();
		} catch (Exception e) {
			progressDialog.dismiss();
			FunctionOfActivity.makeToaskShow(context, "获取运输合同数据异常");
		}
		return result;
	}

	/**
	 * 提交同意合同
	 * 
	 * @param context
	 * @param param
	 */
	public static void submitElectronMainfestAgree(Context context,
			Map<String, Object> param) {
		// TODO Auto-generated method stub
		thisContext = context;
		progressDialog = new ProgressDialog(thisContext);
		progressDialog.setMessage("正在提交，请稍候...");
		progressDialog.show();
		try {
			new Thread(new BizNetworkService(context, handler,
					HttpUrlConstant.AGREE_ELECTRO, param)).start();
		} catch (Exception e) {
			progressDialog.dismiss();
			FunctionOfActivity.makeToaskShow(context, "提交同意合同异常");
		}
	}

	/**
	 * 提交不同意合同
	 * 
	 * @param context
	 * @param param
	 */
	public static void submitElectronMainfestNotAgree(Context context,
			Map<String, Object> param) {
		// TODO Auto-generated method stub
		thisContext = context;
		progressDialog = new ProgressDialog(thisContext);
		progressDialog.setMessage("正在提交，请稍候...");
		progressDialog.show();
		try {
			new Thread(new BizNetworkService(context, handler,
					HttpUrlConstant.DISAGREE_ELECTRO, param)).start();
		} catch (Exception e) {
			progressDialog.dismiss();
			FunctionOfActivity.makeToaskShow(context, "提交不同意合同异常");
		}
	}

	/**
	 * 获取确认签单内容
	 * 
	 * @param context
	 */
	public static String getConfirmOrderDatas(Context context) {
		// TODO Auto-generated method stub
		thisContext = context;
		try {// 此处try catch是为了处理个列表下拉刷新生成了新的线程，而UI在线程中会报错，导致刷新失败
			progressDialog = new ProgressDialog(thisContext);
			progressDialog.setMessage("正在加载，请稍候...");
			progressDialog.show();
		} catch (Exception e) {
		}
		try {
			new Thread(new BizNetworkService(context, handler,
					HttpUrlConstant.CONFIRMLIST, null)).start();
		} catch (Exception e) {
			progressDialog.dismiss();
			FunctionOfActivity.makeToaskShow(context, "获取确认签单数据异常");
		}
		return result;
	}

	/**
	 * 获取船东资料,不需要handler处理
	 * 
	 * @param context
	 */
	public static void getShipOwnerInformation(Context context, Handler mHandler) {
		// TODO Auto-generated method stub
		thisContext = context;
		try {
			SharePreferenceUtil sp = new SharePreferenceUtil(context,
					HttpConstant.SAVE_USER);
			Map<String, Object> params2 = new HashMap<String, Object>();
			params2.put("ship_id", sp.getShip_id());
			new Thread(new BizNetworkService(context, mHandler,
					HttpUrlConstant.INIT_CONTACT, params2)).start();
		} catch (Exception e) {
			progressDialog.dismiss();
		}
	}

	/**
	 * 提交船东资料
	 * 
	 * @param context
	 * @param param
	 * @param mHandler
	 */
	public static void confirmShipOwnerInfo(Context context,
			Map<String, Object> param, Handler mHandler) {
		// TODO Auto-generated method stub
		thisContext = context;
		try {
			new Thread(new BizNetworkService(context, mHandler,
					HttpUrlConstant.SHIP_OWNER, param)).start();
		} catch (Exception e) {
			progressDialog.dismiss();
			FunctionOfActivity.makeToaskShow(context, "提交资料异常");
		}
	}

	/**
	 * 获取确认签单的图片
	 * 
	 * @param context
	 * @param inStringValue
	 * @param type
	 */
	public static String getPictureOfConfirmOrder(Context context,
			String inStringValue, String type) {
		// TODO Auto-generated method stub
		thisContext = context;
		try {// 此处try catch是为了处理个列表下拉刷新生成了新的线程，而UI在线程中会报错，导致刷新失败
			progressDialog = new ProgressDialog(thisContext);
			progressDialog.setMessage("正在加载，请稍候...");
			progressDialog.show();
		} catch (Exception e) {
		}
		try {
			Map<String, Object> param2 = new HashMap<String, Object>();
			param2.put("eleManifestId", inStringValue);
			param2.put("jumpType", type);
			new Thread(new BizNetworkService(context, handler,
					HttpUrlConstant.CONFIRM_SIGN_INT, param2)).start();
		} catch (Exception e) {
			progressDialog.dismiss();
			FunctionOfActivity.makeToaskShow(context, "获取确认签单图片异常");
		}
		return result;

	}

	/**
	 * 提交签单
	 * 
	 * @param context
	 * @param infoCount
	 * @param inStringValue
	 */
	public static void submitConfirmOrder(Context context, String infoCount,
			String inStringValue) {
		// TODO Auto-generated method stub
		// 提交签单前需要到获取图片的接口拿到id和submitOrderId
		try {
			thisContext = context;
			// TODO Auto-generated method stub
			progressDialog = new ProgressDialog(thisContext);
			progressDialog.setMessage("正在提交，请稍候···");
			progressDialog.show();
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("shipperWeight", infoCount);
			params.put("eleManifestId", inStringValue);
			new Thread(new BizNetworkService(context, handler,
					HttpUrlConstant.SUBMIT_CONRIME_ORDER, params)).start();
		} catch (Exception e) {
			// TODO: handle exception
			progressDialog.dismiss();
			FunctionOfActivity.makeToaskShow(context, "提交异常");

		}
	}

	/**
	 * 运输合同任务提醒
	 * 
	 * @param context
	 * @param getPushValueOfKey
	 */
	public static void getPushMessageAndReturnEleActivity(Context context,
			String getPushValueOfKey) {
		// TODO Auto-generated method stub
		thisContext = context;
		thisContext.startActivity(new Intent(thisContext,
				ElectronManifestActivity.class));
	}

	/**
	 * 获取消息数
	 * 
	 * @param context
	 * @param status
	 * @param page
	 * @return
	 */
	public static String getNewsDatas(Context context, String status,
			String page, Handler thisHandler) {
		// TODO Auto-generated method stub
		thisContext = context;
		try {// 此处try catch是为了处理个列表下拉刷新生成了新的线程，而UI在线程中会报错，导致刷新失败
			SharePreferenceUtil sp = new SharePreferenceUtil(context,
					HttpConstant.SAVE_USER);
			Map<String, Object> param2 = new HashMap<String, Object>();
			param2.put("ship", sp.getShip_id());
			param2.put("status", status);
			param2.put("page", page);
			new Thread(new BizNetworkService(context, thisHandler,
					HttpUrlConstant.GET_MESSAGE_LIST, param2)).start();
		} catch (Exception e) {
			progressDialog.dismiss();
			FunctionOfActivity.makeToaskShow(context, "获取异常");
		}
		return result;
	}

	/**
	 * 申请取消合同
	 * 
	 * @param context
	 * @param param
	 */
	public static void submitElectronMainfestRequestCancel(Context context,
			Map<String, Object> param) {
		thisContext = context;
		progressDialog = new ProgressDialog(thisContext);
		progressDialog.setMessage("正在提交，请稍候...");
		progressDialog.show();
		try {
			new Thread(new BizNetworkService(context, handler,
					HttpUrlConstant.SUBMIT_REQUEST_CANCEL_ELE, param)).start();
		} catch (Exception e) {
			progressDialog.dismiss();
			FunctionOfActivity.makeToaskShow(context, "申请取消合同异常");
		}

	}

}
